<?php


require_once(DIR_LIB."vslibdef.php");

if(!defined('DIR_LIB_VSCORE'))
	define('DIR_LIB_VSCORE', DIR_LIB."VsCore".DIR_SEP);

if(!defined('DIR_LIB_VSSHOP'))
	define('DIR_LIB_VSSHOP', DIR_LIB."VsShop".DIR_SEP);

require_once(DIR_LIB_VSCORE."vsapplication.php");

require_once(DIR_LIB_VSSHOP."vsproduct_factory.php");


class VsOrderItemFactory {


	static function makeOrderItems($orderId=0) {
		$orderItems = array();

		if($orderId) {
			$q = "SELECT * FROM `orders_items` WHERE orders_id=".$orderId;
			$res = $db->query($q);
			while($order = mysql_fetch_object($res)) {

			}
		}

		return $orderItems;
	}
	//================================================================================

};
//=============================================================================================================



class VsOrderItem{

	protected $_sqlTable = 'Unnamed';

	protected $_id = 0;
	protected $_order = NULL;

	protected $_product = NULL;	// VsProduct Object
	protected $_qty = 0;

	protected $_price = 0;	// Edini4na Cena

	function VsOrderItem(&$order, $productId, $qty) {
		$this->_sqlTable = "order_items";
		//echo "Color Id: ".$colorId;

		$this->_order = &$order;

		$productFactory = VsProductFactory::Instance();
		$this->_product = $productFactory->makeProduct($productId);
		$this->_qty = $qty;

		$this->_price = is_object($this->_product) ? $this->_product->price() : 0; 	// Edini4na Cena
	}
	//==============================================================================

	function itemId() { return $this->_id; }	// Ne e Vqrno

	function id() { return $this->_product->id(); }
	function orderId() { return $this->_order->id(); }

	function productId() { return $this->_product->id(); }
	function productName() { return is_object($this->_product) ? $this->_product->name() : NULL; }
	function name() { return $this->_product->name(); }
	function image() { return $this->_product->image(); }
	function productImage() { return $this->_product->image(); }
	function productPrice() { return $this->_product->price(); }
	function price() { return $this->_price; }
	function setPrice($price) { $this->_price = $price; }
	function total() { return $this->_price * $this->_qty; }
	//==============================================================================

	function qty() { return $this->_qty; }
	function addQty($qty) { $this->_qty += $qty; }
	//=============================================================================

	function save() {
		//echo "VsProduct::save()";

		if($this->_order->id() < 1)
			return;

		if($this->_id > 0)
			$this->update();
		else
			$this->insert();
	}
	//==============================================================================

	function delete()  {
		if(!$this->_id)
			return FALSE;

		$q = "DELETE FROM `".$this->_sqlTable."` WHERE id=".$this->_id;

		$this->_db->query($q);

		return TRUE;
	}
	//==============================================================================

	protected function insert()  {
		$db = VsDatabase::Instance();


		$q = "INSERT INTO `".$this->_sqlTable."` (`products_categories_id`, `name`, `info`, `date`) VALUES('".$this->_categoryId."', '".addslashes($this->_name)."', '".addslashes($this->_info)."', NOW())";
		//echo $q;

		$db->query($q);
		//echo mysql_error();

		$this->_id = mysql_insert_id();
	}
	//==============================================================================

	protected function update()  {
		$db = VsDatabase::Instance();


		$q = "UPDATE `".$this->_sqlTable."` SET `products_categories_id`='".$this->_categoryId."', `name`='".addslashes($this->_name)."', `info`='".addslashes($this->_info)."', date=NOW() WHERE id=".$this->_id;

		$db->query($q);
	}
	//==============================================================================

	
};
